<template>
	<view>
		<view class="item" style="margin-top: 40rpx;">
			<view class="item_title">注意事项</view>
			<view class="item_content">
				1.由于小程序的限制，无法直接下载文件，只能采用曲线救国的方式下载。
			</view>
			<view class="item_content">
				2.安卓(android)手机可以在打开文档后，点击右上角菜单(三点图标)，点击"保存到手机"即可。
			</view>
			<view class="item_content">
				3.苹果(ios)手机可以在打开文档后，点击右上角菜单(三点图标), 先发送给朋友(建议发送给文件传输助手)，再进行保存
			</view>

		</view>

<!-- 		<ad style="padding-bottom: 30rpx;" unit-id="j9b70ufbgfk05cp7ug"></ad> -->

		<view class="submit_button" :style="{backgroundColor: mainColor}" @click="exportWeight">导出体重数据</view>
	</view>
</template>

<script>
	import toastUtil from '@/utils/toastUtil.js'
	import storageUtil from '@/utils/storageUtil.js'


	const app = getApp()
	export default {
		data() {
			return {

				// 系统平台
				system: app.globalData.system,
				// 导航栏高度
				navigationHeight: app.globalData.navigationHeight,
				// 统一背景颜色
				unifyBgColor: app.globalData.unifyBgColor,
				// 顶部导航栏高度
				topNavigationHeight: app.globalData.topNavigationHeight,
				// 主色调
				mainColor: app.globalData.mainColor,
				// 临时文件地址
				tempFileUrl: null
			}
		},
		methods: {

			/**
			 * 导出体重数据
			 */
			exportWeight() {
				console.log('exportWeight..............')
				if (this.tempFileUrl) {
					this.openExcel(this.tempFileUrl)
					return
				}
				uni.showLoading({
					title: '加载中'
				})
				uniCloud.callFunction({
					name: "exprotWeight",
					data: {
						openid: storageUtil.getOpenId()
					},
					complete: res => {
						console.log('exportWeight.....complete.........')
						console.log(res)

						// this.getFileUrl(res.result.fileID)
						let filePath = res.result.fileID
						console.log('weight_diary_wx.cloud.downloadFile... start.....')
						uni.showLoading({
							title: '加载中'
						})
						uni.downloadFile({
							url: filePath,
							success: res => {
								console.log(
									'weight_diary_wx.cloud.downloadFile... success..... res : ',
									res)
								// get temp file path
								console.log(res.tempFilePath)
								let tempFilePath = res.tempFilePath
								this.tempFileUrl = tempFilePath
								this.openExcel(tempFilePath)

							},
							fail: err => {
								console.log(
									'weight_diary_wx.cloud.downloadFile... success..... err : ',
									err)
								// handle error
								console.log(err)
							},
							complete: res => {
								console.log(
									'weight_diary_wx.cloud.downloadFile... complete..... res : ',
									res)
								// handle error
								console.log(res)
								toastUtil.hideLoading()
							}
						})

						toastUtil.hideLoading()
					}
				})
			},


			/**
			 * 打开excel
			 */
			openExcel(tempFilePath) {
				uni.showLoading({
					title: '加载中'
				})
				wx.openDocument({
					showMenu: true,
					filePath: tempFilePath,
					success: ress => {
						console.log('打开文档成功')
					},
					fail: res => {
						toastUtil.showToast('打开文档失败')
					},
					complete: res => {
						console.log('weight_diary_wx.cloud.downloadFile... complete..... res : ', res)
						// handle error
						console.log(res)
						toastUtil.hideLoading()
					}
				})
			}
		}
	}
</script>

<style>
	.item {
		width: calc(100% - 40rpx);
		padding-left: 20rpx;
		padding-right: 20rpx;
		padding-bottom: 30rpx;
	}

	.item_title {
		font-weight: bold;

	}

	.item_content {
		padding-top: 10rpx;
	}

	/*  提交按钮  */
	.submit_button {
		height: 80rpx;
		line-height: 80rpx;
		width: 250rpx;
		background: -webkit-linear-gradient(top, rgb(99, 200, 251), rgb(49, 165, 245));
		display: flex;
		justify-content: center;
		align-items: center;
		color: white;
		border-radius: 10rpx;
		margin-top: 30rpx;
		margin-left: calc(50vw - 125rpx);
	}
</style>
